﻿using Microsoft.Extensions.Logging;
using NetCoreCMS.IServices;
using NetCoreCMS.Services;
using Quartz;
using System;
using System.IO;
using System.Threading.Tasks;

namespace NetCoreCMS.Job
{
    [DisallowConcurrentExecution]
    public class LogTestJob : IJob
    {
        private readonly ITask_logService _log;
        public LogTestJob(ITask_logService log)
        {
            this._log = log;
        }
        private readonly string AppUrl = AppDomain.CurrentDomain.BaseDirectory;
        public async Task Execute(IJobExecutionContext context)
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            string serverName = dataMap.GetString("ServerName");
            if (string.IsNullOrEmpty(serverName))
            {
                serverName = "netcorecms";
            }
            try
            {
                _log.Add(new Models.Task_log {task_name = serverName, remark = "task" });
            }
            catch (Exception ex)
            {
                File.AppendAllText(AppUrl + "\\QuartzLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", $"Error:{DateTime.Now.ToString()}\n\t{serverName}:{ex.Message}\n");
            }
            await Task.CompletedTask;
        }
    }
}
